use glam::{Vec3, vec3};
use use_cudarc::{
    camera::Camera,
    cuda_renderer::CudaRenderer,
    framebuffer_renderer::FrameBufferRenderer,
    model::{GpuModel, Model},
    window_app::App,
};

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let model_dir = &args[1];
    assert!(std::fs::exists(model_dir).unwrap());

    let fb_size = (1080, 1080);
    let camera = Camera {
        position: vec3(-1., 0., 2.),
        center: Vec3::ZERO,
        up: Vec3::Y,
        fov: 1.,
        aspect_ration: 1.,
        take_snap: true,
    };
    let mut renderer = CudaRenderer::new(fb_size, camera).unwrap();

    for entry in std::fs::read_dir(model_dir).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();

        if path.is_file() && path.extension().map(|ext| ext == "obj").unwrap_or(false) {
            renderer.add_model(
                GpuModel::from_model(
                    renderer.device().default_stream(),
                    &Model::from_file(&path).unwrap(),
                )
                .unwrap(),
            );
        }
    }

    let mut app = App::new(
        Box::new(move |display| -> FrameBufferRenderer<CudaRenderer> {
            FrameBufferRenderer::<CudaRenderer>::new(display, fb_size, renderer)
        }),
        "PIPELINE RENDER",
        fb_size,
    );

    glium::winit::event_loop::EventLoop::new()
        .unwrap()
        .run_app(&mut app)
        .unwrap()
}
